/***************************************************************************
 *   Copyright (C) 2008/2009 by Philipp Müller                             *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QtCore>
#include <QPainter>

#include "qtvomp.h"
#include "widgetepgheader.h"

WidgetEpgHeader::WidgetEpgHeader( QWidget *parent ) : QWidget( parent ),
    m_stretch( 4 ),
    m_logoSpace( 200 )
{
    setMinimumSize( 0, 20 );
}


WidgetEpgHeader::~WidgetEpgHeader()
{
}


void WidgetEpgHeader::paintEvent( QPaintEvent * event )
{
    QPainter painter( this );
    int width = size().width() - 1;
    int height = size().height() - 1;
    int start = m_logoSpace;
    QRect rect;
    QDateTime displayTime = epgVisibleTime();

    rect.setCoords( 0, 0, m_logoSpace, height );
    painter.drawText( rect, Qt::AlignLeft | Qt::TextSingleLine | Qt::AlignVCenter, displayTime.toString( "ddd dd.MM" ) );

    while ( start < width )
    {
        rect.setCoords( start - 30, 0, start + 30, height );
        painter.drawText( rect, Qt::AlignCenter | Qt::TextSingleLine, displayTime.toString( "hh:mm" ) );
        start += 30 * 4;
        displayTime = displayTime.addSecs( 30 * 60 );
    }

    QWidget::paintEvent( event );
}


void WidgetEpgHeader::updateVisibleTime()
{
    repaint();
}


void WidgetEpgHeader::mousePressEventX( int x )
{
    m_epgSelectionTime = m_epgVisibleTime.addSecs( ( x - m_logoSpace ) * 60 / stretch() );
}


void WidgetEpgHeader::setEpgVisibleTime( QDateTime dateTime )
{
    if( dateTime < m_mainWindow->serverTime() )
    {
        dateTime = m_mainWindow->serverTime();
    }

    QTime time = dateTime.time();

    if ( time.minute() < 30 )
    {
        dateTime = dateTime.addSecs( -time.minute() * 60 - time.second() );
    }
    else
    {
        dateTime = dateTime.addSecs( ( -time.minute() + 30 ) * 60 - time.second()  );
    }
    m_epgVisibleTime = dateTime;
}


int WidgetEpgHeader::currentEpgItem( int row )
{
    return m_mainWindow->channelList()->rowOfEpgItem( row, epgSelectionTime() );
}


void WidgetEpgHeader::selectLeft( int row )
{
    int scheduleNumber = currentEpgItem( row );

    if( scheduleNumber < 1 )
    {
        setEpgSelectionTime( m_mainWindow->serverTime() );
    }
    else
    {
        setEpgSelectionTime( m_mainWindow->channelList()->at( row ).scheduleList[ scheduleNumber - 1 ].time );
    }

    if( epgSelectionTime() < epgVisibleTime() ) // are we now out of screen?
    {
        qDebug() << "WidgetEpgHeader::selectLeft: sroll to left needed with scheduleNumber: " << scheduleNumber;
        setEpgVisibleTime( epgSelectionTime() );

        m_model->epgDataChanged( -1 );
        update();
    }
    else
    {
        m_model->epgDataChanged( row );
    }
}


void WidgetEpgHeader::selectRight( int row )
{
    int listSize = m_mainWindow->channelList()->at( row ).scheduleList.size();
    int scheduleNumber = currentEpgItem( row );

    if ( scheduleNumber < 0 ) //nothing found, so either empty or the first entry
    {
        if ( listSize > 1 )
        {
            setEpgSelectionTime( m_mainWindow->channelList()->at( row ).scheduleList[ 1 ].time );
        }
        else
        {
            setEpgSelectionTime( m_mainWindow->serverTime() );
        }
    }
    else if( scheduleNumber == listSize - 1 ) //this is the last entry
    {
        if( listSize > 0 )
        {
            setEpgSelectionTime( m_mainWindow->channelList()->at( row ).scheduleList[ listSize - 1 ].time );
        }
        else
        {
            setEpgSelectionTime( m_mainWindow->serverTime() );
        }
    }
    else
    {
        setEpgSelectionTime( m_mainWindow->channelList()->at( row ).scheduleList[ scheduleNumber + 1 ].time );
    }

    if( epgSelectionTime() >= epgMaxVisibleTime() )
    {
        qDebug() << "WidgetEpgHeader::selectRight: sroll to right needed with scheduleNumber: " << scheduleNumber;
        setEpgVisibleTime( epgSelectionTime() );

        m_model->epgDataChanged( -1 );
        update();
    }
    else
    {
        m_model->epgDataChanged( row );
    }
}


QDateTime WidgetEpgHeader::epgMaxVisibleTime()
{
    return epgVisibleTime().addSecs( ( size().width() - m_logoSpace - 1 ) * 60 / stretch() );
}
